#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main(int argc, char **argv)
{
	int size, rank, i, inbuf, outbuf;
	MPI_Request *requests;
	MPI_Status *statuses;

	MPI_Init(&argc, &argv);
	
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	
	if (rank == 0)
	{
		requests = (MPI_Request *) malloc((size - 1) * sizeof(MPI_Request));
		statuses = (MPI_Status *) malloc((size - 1) * sizeof(MPI_Status));
		for (i = 1; i < size; i++)
		{
			if (i == rank) continue;
			MPI_Isend(&outbuf, 1, MPI_INT, i, 1, MPI_COMM_WORLD, requests + (i - 1));
		}
		MPI_Waitall(size - 1, requests, statuses);
	}
	else {
		MPI_Recv(&inbuf, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
	}
	
	MPI_Finalize();
	printf("rank %d finished\n", rank);
}